<template>
  <div class="order-index">
    <el-form
      ref="form"
      :inline="true"
      :model="form"
      class="search-form"
    >
      <el-form-item
        label="创建时间"
      >
        <el-date-picker
          v-model="date"
          size="small"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="订单状态">
        <el-select
          v-model="form.order_state"
          size="small"
          placeholder="订单状态">
          <el-option
            v-for="item in orderStatusList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单来源">
        <el-select
          v-model="form.order_from"
          size="small"
          placeholder="订单来源"
        >
          <el-option
            v-for="item in orderSource"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="司机姓名">
        <el-input
          v-model="form.driver"
          size="small"
          placeholder="请输入司机姓名，可选"
        ></el-input>

      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          @click="search(1)"
        >查询
        </el-button>
      </el-form-item>
    </el-form>
    <!--    <div class="operate-bar">-->
    <!--      <el-button-->
    <!--        size="small"-->
    <!--        @click="exportExcel"-->
    <!--      >导出excel-->
    <!--      </el-button>-->
    <!--    </div>-->
    <el-table
      :data="tableData"
      style="width: 100%"
      v-loading="loading"
      stripe
      :show-summary="true"
      :summary-method="getSummaries"
    >
      <el-table-column
        prop="from"
        label="订单来源"
        width="140"
      >
      </el-table-column>
      <el-table-column
        prop="driver"
        label="司机名称"
      >
      </el-table-column>
      <el-table-column
        prop="money"
        label="订单价格"
        width="95"
      >
      </el-table-column>
      <el-table-column
        prop="state_text"
        label="订单状态"
        width="95"
      >
      </el-table-column>
      <el-table-column
        prop="create_time"
        label="创建时间"
      >
      </el-table-column>
      <el-table-column
        prop="name"
        label="乘客姓名"
      >
      </el-table-column>
      <el-table-column
        prop="phone"
        label="手机号码"
        width="115"
      >
      </el-table-column>
      <el-table-column
        prop="date"
        label="操作"
        width="138"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="viewOrderDetail(scope.row)"
          >订单明细
          </el-button>
          <el-button
            type="text"
            size="small"
            @click="viewOrderRoute(scope.row)"
          >查看行程
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="total, prev, pager, next, jumper"
        :page-size.sync="form.size"
        :current-page.sync="form.page"
        :total="total"
        :hide-on-single-page="true"
        @current-change="search"
      >
      </el-pagination>
    </div>
    <order-detail></order-detail>
    <order-route></order-route>
  </div>
</template>

<script>
  /**
   * @Author: lifuzhao
   * @Date: 2019-07-11
   * @Project: driver
   */
  import {mapGetters} from 'vuex'
  import {orderSource, orderStatus, pageSize} from "../../constant"
  import orderDetail from './parts/detail'
  import orderRoute from './parts/route'
  import './order.less'

  export default {
    name: "index"
    , data() {
      return {
        orderSource
        , orderStatus
        , form: {
          page: 1
          , size: pageSize
          , time_begin: ''
          , time_end: ''
          , order_state: 6
          , order_from: 5
          , driver: ''
        }
        , date: []
        , tableData: []
        , loading: false
        , total: 0
      }
    }
    , computed: {
      ...mapGetters([
        'gradeText'
      ])
      , orderStatusList() {
        let gradeText = this.gradeText
        if (gradeText !== '管理员') {
          return [{
            label: '完成'
            , value: 4
          }]
        }
        return orderStatus
      }
    }
    , watch: {
      date(date) {
        let start = ''
          , end = ''
        if (date) {
          [start, end] = date
        }
        this.form.time_begin = start
        this.form.time_end = end
      }
      , $route: {
        deep: true
        , immediate: true
        , handler() {
          this.search(1)
        }
      }
      , gradeText: {
        deep: true
        , immediate: true
        , handler(gradeText) {
          if (gradeText !== '管理员') {
            this.form.order_state = 4
          } else {
            this.form.order_state = 6
          }
        }
      }
    }
    , methods: {
      search(page) {
        this.form.page = page
        this.getList()
      }
      , getList() {
        this.loading = true
        this.$axios
          .get('/api/v1/orders/manager/cms', {
            params: this.form
            , abort: true
          })
          .then(res => {
            this.loading = false
            let tableData = []
              , {statistic = {}, total = 0, data = []} = res
            if (res.success) {
              tableData = (data || []).map(item => {
                orderStatus.forEach(status => {
                  if (status.value === item.state) {
                    item.state_text = status.label
                  }
                })
                return item
              })
            }
            this.tableData = tableData
            this.statistic = statistic
            this.total = total
          })
          .catch(() => {
            this.loading = false
            this.tableData = []
          })
      }
      , getSummaries() {
        if (this.tableData.length === 0)
          return []
        return [
          '代驾人数'
          , this.statistic.members
          , '订单总数'
          , this.statistic.orders_count
          , '费用总计'
          , this.statistic.all_money
        ]
      }
      , exportExcel() {

      }
      , viewOrderDetail(order) {
        this.$bus.$emit('viewOrderDetail', order)
      }
      , viewOrderRoute(order) {
        this.$bus.$emit('viewOrderRoute', order)
      }
    }
    , components: {
      orderDetail
      , orderRoute
    }
  }
</script>

<style lang="less">
  .order-index {

  }
</style>
